<script type="text/javascript">
$$(document).ready(function() {
    get_processes();
    $$("#get-processes").click(function(){
        get_processes();
    });
});

function get_processes(){
     $$("#systemd-spinner").css("display", "block");
     $$(".systemd-node").remove()
     $$.post( "/ajax", { 
        'func': "systemd_get_processes"
    }).done(function( data ) {
        $$("#systemd-spinner").css("display", "none");
        for(var i in data.processes){
            $$("#processes").append(make_process(data.processes[i]))
            $$("#processes").append(make_process_info(data.processes[i]))
        }
    });
}

function restart_process(unit){
    $$.post( "/ajax", { 
        'func': "systemd_restart",
        'unit': unit
    }).done(function( data ) {
        if(data.ok){
            s = $$('#status-'+unit.substring(0,unit.indexOf(".")))
            s.html(data.status)
            s.effect( "highlight", 
                {color:"green"}, 1000 );
        }
    });
}

function update_process(unit){
    $$.post( "/ajax", { 
        'func': "systemd_get_status",
        'unit': unit
    }).done(function( data ) {
        if(data.ok){
            s = $$('#status-'+unit.substring(0,unit.indexOf(".")))
            s.html(data.status)
            s.effect( "highlight", 
                {color:"green"}, 1000 );
        }
    });
}

function get_status(panel){
    $$.post( "/ajax", { 
        'func': "systemd_get_status", 
        'unit': panel.attr("process")
    }).done(function( data ) {
        id = data.unit.substring(0,data.unit.indexOf("."));
        var td = $$("<td colspan='4'><pre id='status-"+id+"'>"+data.status+"</pre></td>")
        var r_button = $$("<button type='button' unit='"+data.unit+"'>Restart</button>")
        r_button.click(function(){
            restart_process($$(this).attr("unit"))
        });
        td.append(r_button)
        var u_button = $$("<button type='button' unit='"+data.unit+"'>Update</button>")
        u_button.click(function(){
            update_process($$(this).attr("unit"))
        });
        td.append(u_button)
        panel.html(td)
    });
}

function active_to_color(act){
    if(act == "active"){
        return "green"
    }
    else if (act == "failed"){
        return "red";
    }
    else{
        return "black";
    }
}

function make_process(p){
    var tr = $$("<tr></tr>")
    tr.attr("class", "systemd-node")
    var name = $$("<td>"+p.unit+"</td>")
    name.css("cursor", "pointer");
    name.click(function(){    
        var panel = $$(this).parent().next();
        var isOpen = panel.is(':visible');
        if(isOpen){
            panel.slideUp();
        }
        else{
            panel.slideDown();
            panel.html("<td colspan='4'>Getting process info...</td>");
            get_status(panel)
        }

        // stop the link from causing a pagescroll
        return false;
    })
    tr.append(name);    
    tr.append("<td>"+p.load+"</td>");
    tr.append("<td style='color: "+active_to_color(p.active)+";'>"+p.active+"</td>");
    tr.append("<td>"+p.sub+"</td>");
    return tr
}

function make_process_info(process){
    info = $$("<tr class='systemd-node' process='"+process.unit+"'><td colspan='4'>Getting process info...</td></tr>")
    info.css("display", "none")
    return info
}

</script>

<div>
    <h3>Systemd</h3>
    <p>These are the processes running on the board</p>
    <button type="button" id="get-processes">Update</button>
    <img id="systemd-spinner" src="static/images/spinner_squares_circle.gif" style="display: block; margin-left: auto; margin-right: auto;">
    <table id="processes" style="font-size: small">
        <tr>
            <th>Unit</th>
            <th>Load</th>
            <th>Active</th>
            <th>Sub</th>
        </tr>
    </table>
</div>
